# Replication materials ---------------------------------------------------
# Paper:    Campaigns and the Selection of Policy-seeking Representatives.
#           Legislative Studies Quarterly
# Authors: Shaun Bowler, Gail McElroy, Stefan Müller


#--------------------------------------------------------------------------
# Script for creating master data (German Bundestag 16, 2005-2009)
#--------------------------------------------------------------------------


# Load necessary packages -------------------------------------------------
library(car)
library(foreign) 
library(Hmisc) 
library(tidyverse)

# Import files into R environment -----------------------------------------

# To make the dataset as anonymous as possible, in Stata we selected only the variables 
# that are absolutely necessary for the analysis. For replication, use the following command in Stata:
 
# keep wkvslist a7_1_05 a7_1_02 a7_2_05 a7_2_02 a8_1_05 a8_2_05 a8_2_02 vb2b1 vb3b2_1 vb3b2_2 
 # vb3b2_3 vb3b2_4 vb3b2_5 vb3b2_6 vb3b2_7 vb3b2_8 vb3b2_9 vb3b2_10 wk_b3_11 l_b2_11 wk_b3_12 
 # l_b2_12 wk_b3_13 l_b2_13 vb3b2_14 vb3b2_15 vb3b2_16 vb3b2_17 vb3b2_18 vb4b3_1 vb4b3_2 vb4b3_3 
 # vb4b3_4 vb4b3_5 vb4b3_6 vb4b3_7 vb8b7_1 vb8b7_2 vb8b7_3 vb8b7_4 vb8b7_5 vb8b7_6 vb8b7_7 
 # vb10b9_3 vb13b12 c3 e1 e2 name geburtsj partei land wkr

# Afterwards, file was exported as a csv file and the Umlaute changed manually as follows:
# ä to ae, ö to oe, ü to ue, Ä to Ae, Ö to Oe, Ü to Ue, and ß to ss.
# The changed names were called "name_merge". This procedure avoids mistakes in merging due to coding errors.

# Import subset of candidate study
# NOTE: THIS FILE CANNOT BE INCLUDED IN THE REPLICATION MATERIAL
GCS16 <- read.csv("data_germany_2005-2009/GCS2005_small.csv", sep = ",", na.strings=c("NA","NaN", " "), fileEncoding = "latin1")


# Recode variable fraktion to factor
GCS16$fraktion <- car::recode(GCS16$partei, "1='SPD'; 2='CDU/CSU'; 3='CDU/CSU'; 4='FDP'; 5='GRUENE'; 6='LINKE'")

# Recode CSU/CSU and SPD to value 1
GCS16$bigparty <- car::recode(GCS16$fraktion, "'SPD'=1; 'CDU/CSU'=1; else=0")

# Include seats and seatshare of factions
BT16_seatshare <- read.csv("data_germany_2005-2009/bt_16_seatshare.csv")

GCS16_seats <- left_join(GCS16, BT16_seatshare, by = "fraktion" )

# Import the legislative activity file

BT16_activity <- read.csv("data_germany_2005-2009/bt_16_activity.csv")

BT16_activity$wonseat <- 1 

# Import the file with personal information about the MPs
BT16_information <- read.csv("data_germany_2005-2009/bt_16_politicians.csv", fileEncoding = "latin1")

# Merge these two files using "name_merge"
BT16_MPs <- left_join(BT16_activity, BT16_information, by = "name_merge")

# Merge GCS16 with BT16_MPs
BT16 <- left_join(GCS16_seats, BT16_MPs,  by = "name_merge") 

# Drop all columns that could be used as personal identifyers (name; name_merged)
BT16 <- dplyr::select(BT16, -c(name, name_merge, Vorname, Nachname))


# Create campaign index ---------------------------------------------------

# Classic campaigning
BT16$use_street <- as.numeric(car::recode(BT16$vb3b2_1, "1=0; 2:7=1"))
BT16$use_calls <- as.numeric(car::recode(BT16$vb3b2_2, "1=0; 2:7=1"))
BT16$use_letters <- as.numeric(car::recode(BT16$vb3b2_3, "1=0; 2:7=1"))
BT16$use_discussions <- as.numeric(car::recode(BT16$vb3b2_4, "1=0; 2:7=1"))
BT16$use_internetdiscussions <- as.numeric(car::recode(BT16$vb3b2_5, "1=0; 2:7=1"))
BT16$use_partymembers <- as.numeric(car::recode(BT16$vb3b2_6, "1=0; 2:7=1"))
BT16$use_partyofficials <- as.numeric(car::recode(BT16$vb3b2_7, "1=0; 2:7=1"))
BT16$use_nateventsattend <- as.numeric(car::recode(BT16$vb3b2_8, "1=0; 2:7=1"))
BT16$use_localeventsattend <- as.numeric(car::recode(BT16$vb3b2_9, "1=0; 2:7=1"))
BT16$use_fundraisingevents <- as.numeric(car::recode(BT16$vb3b2_10, "1=0; 2:7=1"))

# Note: "wk" and "l" distinguishes between constituency and list candidates
BT16$use_wk_appointmentscompanies <- as.numeric(car::recode(BT16$wk_b3_11, "1=0; 2:7=1;99=NA"))
BT16$use_l_appointmentscompanies <- as.numeric(car::recode(BT16$l_b2_11, "1=0; 2:7=1;99=NA"))

BT16$use_wk_appointmentsclubs <- as.numeric(car::recode(BT16$wk_b3_12, "1=0; 2:7=1;99=NA"))
BT16$use_l_appointmentsclubs <- as.numeric(car::recode(BT16$l_b2_12, "1=0; 2:7=1;99=NA"))

BT16$use_wk_organising <- as.numeric(car::recode(BT16$wk_b3_13, "1=0; 2:7=1;99=NA"))
BT16$use_l_organising <- as.numeric(car::recode(BT16$l_b2_13, "1=0; 2:7=1;99=NA"))

summary(BT16$use_wk_organising)
summary(BT16$use_l_organising)
summary(BT16$l_b2_13)

BT16$use_localnewspaper <- as.numeric(car::recode(BT16$vb3b2_14, "1=0; 2:7=1"))
BT16$use_natnewspaper <- as.numeric(car::recode(BT16$vb3b2_15, "1=0; 2:7=1"))

BT16$use_localtvradio <- as.numeric(car::recode(BT16$vb3b2_16, "1=0; 2:7=1"))
BT16$use_nattvradio <- as.numeric(car::recode(BT16$vb3b2_17, "1=0; 2:7=1"))


# Promotion material
# Originally, the variables are coded on a 1-4 scale. 
# Here, we recode "not important at all" (1) and "not very important" (2) to 0
# and "important" (3) and "very important" to 1.
BT16$use_posters <- as.numeric(car::recode(BT16$vb4b3_1, "1:2=0; 3:4=1"))
BT16$use_adsinpress <- as.numeric(car::recode(BT16$vb4b3_2, "1:2=0; 3:4=1"))
BT16$use_officehours <- as.numeric(car::recode(BT16$vb4b3_3, "1:2=0; 3:4=1"))
BT16$use_socialgatherings <- as.numeric(car::recode(BT16$vb4b3_4, "1:2=0; 3:4=1"))
BT16$use_flyers <- as.numeric(car::recode(BT16$vb4b3_5, "1:2=0; 3:4=1"))
BT16$use_spots <- as.numeric(car::recode(BT16$vb4b3_6, "1:2=0; 3:4=1"))

# Post-modern campaigning
BT16$use_website <- as.numeric(car::recode(BT16$vb8b7_2, "1=1; else=0"))
BT16$use_emaillist <- as.numeric(car::recode(BT16$vb8b7_3, "1=1; else=0"))
BT16$use_campaignvideo <- as.numeric(car::recode(BT16$vb8b7_4, "1=1; else=0"))
BT16$use_onlinechats <- as.numeric(car::recode(BT16$vb8b7_5, "1=1; else=0"))

# vb8b7_6: I advertised my webpage and/or email address on campaign literature
# BT16$use_attentiontoweb <- car::recode(BT16$vb8b7_6, "1=1; else=0") # not really an activity
BT16$use_blog <- as.numeric(car::recode(BT16$vb8b7_7, "1=1; else=0"))


# Create additive indices
BT16$use_classic <- rowSums(BT16[,c("use_street",
                                    "use_calls",
                                    "use_letters",
                                    "use_discussions",
                                    "use_partymembers",
                                    "use_partyofficials",
                                    "use_nateventsattend",
                                    "use_localeventsattend",
                                    "use_fundraisingevents",
                                    "use_wk_appointmentscompanies",
                                    "use_l_appointmentscompanies",
                                    "use_wk_appointmentsclubs",
                                    "use_l_appointmentsclubs",
                                    "use_wk_organising",
                                    "use_l_organising",
                                    "use_localnewspaper",
                                    "use_natnewspaper",
                                    "use_localtvradio",
                                    "use_nattvradio")], na.rm=TRUE)


BT16$use_material <- rowSums(BT16[,c("use_posters",
                                     "use_adsinpress",
                                     "use_officehours",
                                     "use_socialgatherings",
                                     "use_flyers",
                                     "use_spots")], na.rm=TRUE)


BT16$use_postmodern <- rowSums(BT16[,c("use_website",
                                       "use_emaillist",
                                       "use_campaignvideo",
                                       "use_onlinechats",
                                       "use_blog",
                                       "use_internetdiscussions")], na.rm=TRUE)



BT16$use_campaign <- rowSums(BT16[,c("use_classic",
                                       "use_material",
                                       "use_postmodern")], na.rm = TRUE)


# Create ordinal index

BT16$vb3b2_1_ord <- as.numeric(car::recode(BT16$vb3b2_1, "NA=1"))
BT16$vb3b2_2_ord <- as.numeric(car::recode(BT16$vb3b2_2, "NA=1"))
BT16$vb3b2_3_ord <- as.numeric(car::recode(BT16$vb3b2_3, "NA=1"))
BT16$vb3b2_4_ord <- as.numeric(car::recode(BT16$vb3b2_4, "NA=1"))
BT16$vb3b2_5_ord <- as.numeric(car::recode(BT16$vb3b2_5, "NA=1"))
BT16$vb3b2_6_ord <- as.numeric(car::recode(BT16$vb3b2_6, "NA=1"))
BT16$vb3b2_7_ord <- as.numeric(car::recode(BT16$vb3b2_7, "NA=1"))
BT16$vb3b2_8_ord <- as.numeric(car::recode(BT16$vb3b2_8, "NA=1"))
BT16$vb3b2_9_ord <- as.numeric(car::recode(BT16$vb3b2_9, "NA=1"))
BT16$vb3b2_10_ord <- as.numeric(car::recode(BT16$vb3b2_10, "NA=1"))

BT16$wk_b3_11_ord <- as.numeric(car::recode(BT16$wk_b3_11, "NA=1"))
BT16$l_b2_11_ord <- as.numeric(car::recode(BT16$l_b2_11, "NA=1"))


BT16$wk_b3_12_ord <- as.numeric(car::recode(BT16$wk_b3_12, "NA=1"))
BT16$l_b2_12_ord <- as.numeric(car::recode(BT16$l_b2_12, "NA=1"))

BT16$wk_b3_13_ord <- as.numeric(car::recode(BT16$wk_b3_13, "NA=1"))
BT16$l_b2_13_ord <- as.numeric(car::recode(BT16$l_b2_13, "NA=1"))

BT16$vb3b2_14_ord <- as.numeric(car::recode(BT16$vb3b2_14, "NA=1"))
BT16$vb3b2_15_ord <- as.numeric(car::recode(BT16$vb3b2_15, "NA=1"))
BT16$vb3b2_16_ord <- as.numeric(car::recode(BT16$vb3b2_16, "NA=1"))
BT16$vb3b2_17_ord <- as.numeric(car::recode(BT16$vb3b2_17, "NA=1"))

BT16$use_ord_total <- (rowSums(BT16[,c("vb3b2_1_ord",
                                      "vb3b2_2_ord",
                                      "vb3b2_3_ord",
                                      "vb3b2_4_ord",
                                      "vb3b2_5_ord",
                                      "vb3b2_6_ord", 
                                      "vb3b2_7_ord",
                                      "vb3b2_8_ord", 
                                      "vb3b2_9_ord", 
                                      "vb3b2_10_ord",
                                      "wk_b3_11_ord",
                                      "l_b2_11_ord",
                                      "wk_b3_12_ord",
                                      "l_b2_12_ord",
                                      "wk_b3_13_ord",
                                      "l_b2_13_ord",
                                      "vb3b2_14_ord",
                                      "vb3b2_15_ord",
                                      "vb3b2_16_ord",
                                      "vb3b2_17_ord")], na.rm=TRUE)) - 20


# Recode and generate legislative activity variables ----------------------

BT16$leg_activity <- rowSums(BT16[,c("Antr", 
                                     "EntschlAntr",
                                     "AendAntr", 
                                     "GesEntw", 
                                     "GrAnfr", 
                                     "KlAnfr"
                                     )], na.rm=TRUE)

BT16$speeches <- BT16$Reden

# Dummy whether candidate has won a seat
BT16$wonseat <- car::recode(BT16$wonseat, "1=1; else=0")

# Create female dummy variable
BT16$female <- as.factor(car::recode(BT16$e1, "1=0; 2=1"))

# List candidate 2005

BT16$list_candidate <- car::recode(BT16$a8_1_05, "1=1; else=0")

BT16$win_constituency <- car::recode(BT16$a7_2_05, "1=1; else=0")


# Calculate age and age squared
BT16$age <- 2005 - BT16$geburtsj

BT16$age_squared <- (BT16$age)^2

# Rename minister variable
BT16$minister <- car::recode(BT16$minister, "1=1; 'NA'=0")


# Rename spending variables
BT16$campaign_budget <- BT16$vb13b12

# Divide budget by 1000
BT16$campaign_budget1000 <- (BT16$vb13b12)/1000

# Create ideology variable sqrt(5 - leftright selfplacement)^2
BT16$ideology <- abs(5 - BT16$c3)

# Create incumbent variable

# Won constituency in 2002
BT16$incumbent_const <- as.numeric(BT16$a7_2_02)

# Won list seat in 2002 
BT16$incumbent_list <- as.numeric(BT16$a8_2_02)

# Add these two variables to "incumbent" variable
BT16$incumbent <- as.numeric(rowSums(BT16[,c("incumbent_const", 
                                     "incumbent_list")], na.rm=TRUE))


# Create dummy for Stadtstaaten (Bremen, Hamburg and Berlin)
BT16$city_state <- car::recode(BT16$land_factor, "'BE'=1; 'HH'=1; 'HB'=1; else=0")


# Standardise variables
BT16$z.use_campaign <- (BT16$use_campaign - mean(BT16$use_campaign, na.rm=TRUE))/(2*sd(BT16$use_campaign, na.rm=TRUE))
BT16$z.use_classic <- (BT16$use_classic - mean(BT16$use_classic, na.rm=TRUE))/(2*sd(BT16$use_classic, na.rm=TRUE))
BT16$z.use_postmodern <- (BT16$use_postmodern - mean(BT16$use_postmodern, na.rm=TRUE))/(2*sd(BT16$use_postmodern, na.rm=TRUE))
BT16$z.use_material <- (BT16$use_material - mean(BT16$use_material, na.rm=TRUE))/(2*sd(BT16$use_material, na.rm=TRUE))
BT16$z.campaign_spending <- (BT16$campaign_budget1000 - mean(BT16$campaign_budget1000, na.rm=TRUE))/(2*sd(BT16$campaign_budget1000, na.rm=TRUE))
BT16$z.age <- (BT16$age - mean(BT16$age, na.rm=TRUE))/(2*sd(BT16$age, na.rm=TRUE))
BT16$z.age_squared <- (BT16$age_squared - mean(BT16$age_squared, na.rm=TRUE))/(2*sd(BT16$age_squared, na.rm=TRUE))
BT16$z.ideology <- (BT16$ideology - mean(BT16$ideology, na.rm=TRUE))/(2*sd(BT16$ideology, na.rm=TRUE))
BT16$z.seat_share <- (BT16$seat_share - mean(BT16$seat_share, na.rm=TRUE))/(2*sd(BT16$seat_share, na.rm=TRUE))


list_candidates <- filter(BT16, list_candidate == 1 | win_constituency ==1)
list_candidates <- dplyr::select(list_candidates, list_candidate, wkvsliste, win_constituency)
cor(list_candidates)


# Keep variables needed for regressions ---

# Exclude variables 1-49 
# These were from original dataset and not necessary for analyses and also ensure anonymity
BT16_subset <- BT16[-c(2:47)]

# Exlude additional variables from the activity file to make it anonymous
delete_vars <- names(BT16_subset) %in% c("PID", "Titel", "second_constituency", "Namenszusatz") 

BT16_final <- BT16_subset[!delete_vars]


bt_16_final <- BT16_final %>% 
  mutate(leg_activity = ceiling(leg_activity)) %>% 
  mutate(government = car::recode(party, "'SPD'='Government'; 'CDU'='Government';'CSU'='Government'; else='Opposition'")) %>% 
  mutate(country_id = "Germany")

# Save this object as a csv file which can be used to replicate all regressions
write_csv(bt_16_final, "BT16_final.csv")



